<template>
    <view>
        <j-cell-group title="当触发条件满足时，场景将会自动执行">
            <j-cell
                v-if="isShowOneKey && isShowTimer && isShowDevice"
                icon="icon-bianji2"
				color="#FF0025"
                title="一键执行"
                sub-title="例如：一键关闭家里所有的灯"
                @jftap="onBindtapSceneType"
            ></j-cell>

            <j-cell 
				v-if="isShowOneKey && isShowTimer" 
				icon="icon-clock" 
				color="#FF0025"
				title="定时器" 
				sub-title="例如：每天早上7点" 
				@jftap="onBindtapTimer"
			></j-cell>

            <j-cell
                v-if="isShowOneKey"
                icon="icon-shebeizhuangtai"
				color="#FF0025"
                title="智能设备状态变化"
                sub-title="例如：当报警器检测到报警时"
                @jftap="onBindtapStatus"
            ></j-cell>
        </j-cell-group>
    </view>
</template>

<script>
// pages/scene/condition/condition.js
export default {
    data() {
        return {
            isShowOneKey: true,
            isShowTimer: true,
            isShowDevice: true
        };
    }
    /**
     * 生命周期函数--监听页面加载
     */,
    onLoad: function (options) {
        console.log('=== scene:condition:onLoad:');
        let scene = uni.getStorageSync('scene'); // getApp().globalData.scene
        // 一键执行

        if (scene.sceneType === 0) {
            this.isShowOneKey = false
        } // 定时器

        for (let i = 0; i < scene.conditionList.length; i++) {
            const item = scene.conditionList[i];

            if (item.conditionType === 1) {
                this.isShowTimer = false
                break;
            }
        } // 设备

        for (let i = 0; i < scene.conditionList.length; i++) {
            const item = scene.conditionList[i];

            if (item.conditionType === 0) {
                this.isShowDevice = false
                break;
            }
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        console.log('=== scene:condition:onReady:');
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        console.log('=== scene:condition:onShow:');
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
        console.log('--- scene:condition:onHide:');
    },
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
        console.log('--- scene:condition:onUnload:');
    },
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {},
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {},
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {},
    methods: {
        // 一键执行
        onBindtapSceneType() {
            // getApp().globalData.scene.sceneType = 0
            // let scene = wx.getStorageSync('scene')
            // scene.sceneType = 0
            // wx.setStorageSync('scene', scene)
            getApp().syncScene(this, {
                sceneType: 0
            });
            uni.navigateBack({
                delta: 1
            });
        },

        // 定时器
        onBindtapTimer() {
            // 与一键执行互斥
            let sceneType = uni.getStorageSync('scene').sceneType; //getApp().globalData.scene.sceneType

            if (sceneType === 0) {
                uni.showModal({
                    title: '提示',
                    content: '一键执行已存在，请先删除一键执行条件',

                    success(res) {
                        if (res.confirm) {
                            uni.navigateBack({
                                delta: 1
                            });
                        }
                    }
                });
                return;
            }

            const conditionList = uni.getStorageSync('scene').conditionList; // getApp().globalData.scene.conditionList

            let bool = false; // 用于判断是否已经存在定时器
            // 已生成定时器

            for (let i = 0; i < conditionList.length; i++) {
                const item = conditionList[i];

                if (item.conditionType === 1) {
                    bool = true;
                    uni.showToast({
                        title: '定时器已存在',
						icon:'none'
                    });
                    break;
                }
            } // 未生成定时器

            if (!bool) {
                let condition = {
                    conditionType: 1,
                    conditionName: '定时器',
                    conditionValue: {
                        cron: '0 30 8 ? * 1,2,3,4,5',
                        timeZone: 'Asia/Shanghai'//Intl.DateTimeFormat().resolvedOptions().timeZone
                    }
                };
                conditionList.push(condition); // getApp().globalData.scene.conditionList = conditionList
                // let scene = wx.getStorageSync('scene')
                // scene.conditionList = conditionList
                // wx.setStorageSync('scene', scene)

                getApp().syncScene(this, {
                    conditionList: conditionList
                });
                uni.navigateBack({
                    delta: 1
                });
            }
        },

        // 智能设备状态变化
        onBindtapStatus(ev) {
            // 与一键执行互斥
            let sceneType = uni.getStorageSync('scene').sceneType; // getApp().globalData.scene.sceneType

            if (sceneType === 0) {
                uni.showModal({
                    title: '提示',
                    content: '一键执行已存在，请先删除一键执行条件',

                    success(res) {
                        if (res.confirm) {
                            uni.navigateBack({
                                delta: 1
                            });
                        }
                    }
                });
                return;
            }

            uni.redirectTo({
                url: '/jvss/pagesScene/condition-status/condition-status'
            });
        }
    }
};
</script>
<style lang="scss">
	// /deep/.cell-group-title{
	// 	max-width: 60vw;
	// }
/* pages/scene/condition/condition.wxss */
</style>
